<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="产品图" min-width="80">
        <template #default="{ row }">
          <el-image
            :src="row.picUrl || row.spu.picUrl"
            class="h-30px w-30px"
            :preview-src-list="[row.picUrl || row.spu.picUrl]"
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="产品名称" min-width="300">
        <template #default="{ row }"> {{ row.name || row.spu.name }}</template>
      </el-table-column>
      <el-table-column align="center" label="销售价" min-width="90">
        <template #default="{ row }"> {{ floatToFixed2(row.price || row.spu.price) }}元</template>
      </el-table-column>
      <el-table-column align="center" label="库存" min-width="90">
        <template #default="{ row }"> {{ row.stock || row.spu.stock }}</template>
      </el-table-column>
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="收藏时间"
        prop="createTime"
        width="180"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as FavoriteApi from '@/api/mall/product/favorite'
import { floatToFixed2 } from '@/utils'

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  createTime: [],
  userId: '' as number | string
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FavoriteApi.getFavoritePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const props = defineProps<{
  userId: number | string
}>()

/** 初始化 **/
onMounted(() => {
  queryParams.userId = props.userId
  getList()
})
</script>
